﻿using Common;
using Common.Aliyun;
using Common.Swagger;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Primitives;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Sample.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    [AllowAnonymous]
    [ApiGroup(ApiGroupNames.Aliyun)]
    public class STSController : ControllerBase
    {
        private readonly IConfiguration configuration;
        public STSController(IConfiguration configuration)
        {
            this.configuration = configuration;
        }
        [HttpGet()]
        [AllowAnonymous]
        public FuncResult Authorize()
        {
            STSAuthorization sts = new STSAuthorization(this.configuration);
            return sts.STSGrant("localtest", null);
        }
        [HttpGet()]
        public FuncResult<string> GrantAccess(string url)
        {
            Request.Headers.TryGetValue("Content-Disposition", out StringValues contentDisposition);
            OSSAccessAuthorize oss = new OSSAccessAuthorize(this.configuration);
            return oss.GrantAccess(url, TimeSpan.FromMinutes(2), contentDisposition.ToString());
        }
        [HttpGet]
        public FileResult Download(string url)
        {
            OSSObjectDownload oss = new OSSObjectDownload(this.configuration);
            var ms = oss.Download(url);
            return File(ms.ToArray(), "image/png");
        }
    }
}
